<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Nnews extends Base{
    //资讯（所有）
    public function pnews(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 503, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(4*($page-1),4)->select();
                if($res){
                    for ($i=0; $i <count($res) ; $i++) { 
                        $res[$i]['times']=date('Y-m-d',$res[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(4*($page-1),4)->select();
                if($res){
                    for ($i=0; $i <count($res) ; $i++) { 
                        $res[$i]['times']=date('Y-m-d',$res[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //资讯（分类名对应的id）
    public function pcatenews(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.cate_id"))){
            $cate_id=input("param.cate_id");
        }else{
            return json(['code' => 503, 'msg' => '请给出分类id']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('cate_id',$cate_id)->where('state',1)->order('id desc')->limit(4*($page-1),4)->select();
                if($res){
                    for ($i=0; $i <count($res) ; $i++) { 
                        $res[$i]['times']=date('Y-m-d',$res[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $res=Db::name('nnews')->where('cus_id',$cus_id)->where('cate_id',$cate_id)->where('state',1)->order('id desc')->limit(4*($page-1),4)->select();
                if($res){
                    for ($i=0; $i <count($res) ; $i++) { 
                        $res[$i]['times']=date('Y-m-d',$res[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$res]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //资讯搜索
    public function pnewssearch(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.key"))){
            $key=input('param.key');
        }else{
            return json(['code' => 503, 'msg' => '搜索关键词呢']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                if($products){
                    for ($i=0; $i <count($products) ; $i++) { 
                        $products[$i]['times']=date('Y-m-d',$products[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('nnews')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                if($products){
                    for ($i=0; $i <count($products) ; $i++) { 
                        $products[$i]['times']=date('Y-m-d',$products[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
}